package com.yue.procress;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.CharArrayWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述: TODO <br/>
 * 创建时间: 2016/10/8<br/>
 *
 * @author QiuYang
 */
public class SimpleBind {
    public static void main(String[] args) {
        readValue();
        System.out.println("*******************");
        writeValue();
    }

    public static void writeValue(){
        Map<String, Object> userData = new HashMap<>();
        ObjectMapper mapper = new ObjectMapper();

        Map<String, String> nameStruct = new HashMap<>();
        nameStruct.put("first", "Joe");
        nameStruct.put("last", "Sixpack");
        userData.put("name", nameStruct);
        userData.put("gender", "MALE");
        userData.put("verified", false);
        userData.put("userImage", "Rm9vYmFyIQ==");

        CharArrayWriter writer = new CharArrayWriter();
        try {
            mapper.writeValue(writer, userData);
            System.out.println(writer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void readValue(){
        Map<String, Object> map = null ;

        ObjectMapper mapper = new ObjectMapper();

        try {
            map = mapper.readValue(SimpleBind.class.getClassLoader().getResourceAsStream("user.json"), Map.class);
            Map<String, String> name = (Map<String, String>) map.get("name");
            System.out.println(name.get("first") + "-" + name.get("last"));
            System.out.println(map.get("gender"));
            System.out.println(map.get("verified"));
            System.out.println(map.get("userImage"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
